#include "IceBlock.h"

#include "Camera.h"
#include "Physics.h"

// Constructor
IceBlock::IceBlock(void)
{
	objectID = "Ice Block";

	SetCurTemp(MinTemp());
}

// Destructor
IceBlock::~IceBlock(void)
{
}

// Update
void IceBlock::Update(float deltaTime)
{
	if(isActive == false)
		return;

	if(CurTemp() > MinTemp() * 0.25f)
		ApplyHeat(5.0f);

	if(CurTemp() > 0.0f)
		this->isActive = false;

	int height = saveHitBox.bottom - saveHitBox.top;
	int y = (int)(height * (1 - (CurTemp() / MinTemp())));

	hitBox.left = saveHitBox.left;
	hitBox.right = saveHitBox.right;
	hitBox.top = saveHitBox.top + y;
	hitBox.bottom = saveHitBox.bottom;

	if(height <= 0)
		isActive = false;
}

// Render
void IceBlock::Render(void) const
{
	if(isActive == false)
		return;

	int width = saveHitBox.right - saveHitBox.left;
	int height = saveHitBox.bottom - saveHitBox.top;
	int y = (int)(height * (1 - (CurTemp() / MinTemp())));

	RECT sect;
	sect.left = 0;
	sect.right = width;
	sect.top = y;
	sect.bottom = height;

	CSGD_TextureManager::GetInstance()->Draw(
		imageID,
		(int)(hitBox.left - Camera::GetInstance()->OffsetX()),
		(int)(hitBox.top - Camera::GetInstance()->OffsetY()),
		1.0f, 1.0f, &sect);

#if DEBUG_COLLISION
	RECT box = hitBox;
	box.left = (int)(box.left - Camera::GetInstance()->OffsetX());
	box.right = (int)(box.right - Camera::GetInstance()->OffsetX());
	box.bottom = (int)(box.bottom - Camera::GetInstance()->OffsetY());
	box.top = (int)(box.top - Camera::GetInstance()->OffsetY());

	CSGD_Direct3D::GetInstance()->DrawHollowRect(box,D3DCOLOR_ARGB(255,255,0,0));
#endif
}

// HandleCollision
void IceBlock::HandleCollision(GameObject& other, CollisionInfo& info)
{
	if(isActive == false)
		return;

	if(info.Side == CollisionSide::BOTTOM)
		other.SetPosY( other.GetPosY() - (info.Overlap.bottom - info.Overlap.top));
	if(info.Side == CollisionSide::TOP)
		other.SetPosY( other.GetPosY() + (info.Overlap.bottom - info.Overlap.top));
	if(info.Side == CollisionSide::LEFT)
		other.SetPosX( other.GetPosX() + (info.Overlap.right - info.Overlap.left));
	if(info.Side == CollisionSide::RIGHT)
		other.SetPosX( other.GetPosX() - (info.Overlap.right - info.Overlap.left));
	other.SetHitBox(Physics::UpdateColRect(other.GetHitBox(), (int)other.GetPosX(), (int)other.GetPosY()));
}

// ResetToCheckPoint
void IceBlock::ResetToCheckPoint()
{

}

void IceBlock::SetSaveHitBox(RECT hitBox)
{
	this->saveHitBox = hitBox;
}

